package com.ruoyi.wm.service.impl;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.wm.domain.WmApparatusInstorageList;
import com.ruoyi.wm.domain.WmApparatusInventory;
import com.ruoyi.wm.domain.WmMedicineInventory;
import com.ruoyi.wm.mapper.WmMedicineInventoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.wm.mapper.WmMedicineInstorageListMapper;
import com.ruoyi.wm.domain.WmMedicineInstorageList;
import com.ruoyi.wm.service.IWmMedicineInstorageListService;
import com.ruoyi.common.core.text.Convert;

/**
 * 药品验收入库记录Service业务层处理
 * 
 * @author hpf
 * @date 2021-11-25
 */
@Service
public class WmMedicineInstorageListServiceImpl implements IWmMedicineInstorageListService 
{
    @Autowired
    private WmMedicineInstorageListMapper wmMedicineInstorageListMapper;

    @Autowired
    private WmMedicineInventoryMapper wmMedicineInventoryMapper;

    /**
     * 查询药品验收入库记录
     * 
     * @param ID 药品验收入库记录ID
     * @return 药品验收入库记录
     */
    @Override
    public WmMedicineInstorageList selectWmMedicineInstorageListById(Long ID)
    {
        return wmMedicineInstorageListMapper.selectWmMedicineInstorageListById(ID);
    }

    /**
     * 查询药品验收入库记录列表
     * 
     * @param wmMedicineInstorageList 药品验收入库记录
     * @return 药品验收入库记录
     */
    @Override
    public List<WmMedicineInstorageList> selectWmMedicineInstorageListList(WmMedicineInstorageList wmMedicineInstorageList)
    {
        return wmMedicineInstorageListMapper.selectWmMedicineInstorageListList(wmMedicineInstorageList);
    }

    /**
     * 新增药品验收入库记录
     * 
     * @param wmMedicineInstorageList 药品验收入库记录
     * @return 结果
     */
    @Override
    public int insertWmMedicineInstorageList(WmMedicineInstorageList wmMedicineInstorageList)
    {
        addIntoInventory(wmMedicineInstorageList);
        return wmMedicineInstorageListMapper.insertWmMedicineInstorageList(wmMedicineInstorageList);
    }

    /**
     * 修改药品验收入库记录
     * 
     * @param wmMedicineInstorageList 药品验收入库记录
     * @return 结果
     */
    @Override
    public int updateWmMedicineInstorageList(WmMedicineInstorageList wmMedicineInstorageList)
    {
        return wmMedicineInstorageListMapper.updateWmMedicineInstorageList(wmMedicineInstorageList);
    }

    /**
     * 删除药品验收入库记录对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteWmMedicineInstorageListByIds(String ids) {
        return wmMedicineInstorageListMapper.deleteWmMedicineInstorageListByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除药品验收入库记录信息
     * 
     * @param ID 药品验收入库记录ID
     * @return 结果
     */
    @Override
    public int deleteWmMedicineInstorageListById(Long ID)
    {
        return wmMedicineInstorageListMapper.deleteWmMedicineInstorageListById(ID);
    }


    /**
     * 添加根据入库记录更新库存功能sercvice
     * @date 2021-11-29
     */
    @Override
    public int updateMedicineStock() {
        return wmMedicineInstorageListMapper.updateStock();
    }


    public void addIntoInventory(WmMedicineInstorageList wmMedicineInstorageList) {
        String name = wmMedicineInstorageList.getName();
        WmMedicineInventory latestInventory = wmMedicineInventoryMapper.selectLatestIndex(name);

        WmMedicineInventory wmMedicineInventory = new WmMedicineInventory();
        wmMedicineInventory.setName(name);//设置名称
        wmMedicineInventory.setForm(wmMedicineInstorageList.getStandard());//设置规格
        wmMedicineInventory.setUnit(wmMedicineInstorageList.getUnit());//设置单位
        wmMedicineInventory.setTime(wmMedicineInstorageList.getTime());//设置时间
        Integer number = wmMedicineInstorageList.getNumber();
        wmMedicineInventory.setInStorageNum(new Long(number));//设置入库数量

        BigDecimal price = wmMedicineInstorageList.getPrice();

        BigDecimal totalPrice = price.multiply(BigDecimal.valueOf(number));//计算入库总价

        wmMedicineInventory.setInPrice(price);
        wmMedicineInventory.setInTotalPrice(totalPrice);
        long remainNum;
        BigDecimal remainPrice;
        if (latestInventory!=null){//如果有上条记录
            long num = latestInventory.getRemainNum();//获取记录的剩余数量
            remainNum=num + wmMedicineInstorageList.getNumber();//剩余数量等于本次加上次
            remainPrice = latestInventory.getRemainTotalPrice().add(totalPrice);
        }
        else {
            wmMedicineInventory.setRemainNum(new Long(number));
            remainNum= wmMedicineInstorageList.getNumber();//剩余数量就是本次入库数量
            remainPrice = totalPrice;
        }
        wmMedicineInventory.setRemainTotalPrice(remainPrice);//计算剩余总价
        wmMedicineInventory.setRemainNum(remainNum);
        wmMedicineInventory.setRemianPrice(price);//设置剩余价格
        wmMedicineInventoryMapper.insertWmMedicineInventory(wmMedicineInventory);
    }
}
